#pragma once

#include <AVUITextBoxBase.h>

namespace AVUI {

class TextBoxView;
class KeyCharEventArgs;

class TextBox : public TextBoxBase
{
public:

    DECLARE_ELEMENT(TextBox, TextBoxBase);

    DECLARE_DEPENDENCY_PROPERTY_BEGIN(Text);
        DECLARE_ATTRIBUTE_CONTENTPROPERTY();
    DECLARE_DEPENDENCY_PROPERTY_END();
    TRefCountedPtr<String> get_Text() { return (String*)(Object*)GetValue(get_TextProperty()); }
    void set_Text(String* pValue) { SetValue(get_TextProperty(), (Object*)pValue); } 

    DECLARE_DEPENDENCY_PROPERTY(TextAlignment);
    TextAlignment::Enum get_TextAlignment() { return (TextAlignment::Enum)(AVUI::UnboxValue<int>(GetValue(get_TextAlignmentProperty()))); }
    void set_TextAlignment(TextAlignment::Enum value) { SetValue(get_TextAlignmentProperty(), AVUI::BoxValue<int>(value)); } 

    DECLARE_DEPENDENCY_PROPERTY(TextWrapping);
    TextWrapping::Enum get_TextWrapping() { return (TextWrapping::Enum)(AVUI::UnboxValue<int>(GetValue(get_TextWrappingProperty()))); }
    void set_TextWrapping(TextWrapping::Enum value) { SetValue(get_TextWrappingProperty(), AVUI::BoxValue<int>(value)); } 

    DECLARE_DEPENDENCY_PROPERTY(IsReadOnly);
    bool get_IsReadOnly() { return AVUI::UnboxValue<bool>(GetValue(get_IsReadOnlyProperty())); }
    void set_IsReadOnly(bool value) { SetValue(get_IsReadOnlyProperty(), AVUI::BoxValue(value)); } 

    DECLARE_DEPENDENCY_PROPERTY(AllowDrop);
    bool get_AllowDrop() { return AVUI::UnboxValue<bool>(GetValue(get_AllowDropProperty())); }
    void set_AllowDrop(bool value) { SetValue(get_AllowDropProperty(), AVUI::BoxValue(value)); } 

    TextBox() : m_caretIndex(0) { }
    virtual ~TextBox() { };


    void set_CaretIndex(int caretIndex);

protected:
    virtual void OnApplyTemplate();
    virtual void OnMouseDown(MouseButtonEventArgs* pArgs);
    virtual void OnKeyDown(KeyEventArgs* pArgs);
    virtual void OnKeyUp(KeyEventArgs* pArgs);
    virtual void OnTextInput(TextCompositionEventArgs* pArgs);

    virtual void OnGotFocus(RoutedEventArgs* pArgs);
    virtual void OnLostFocus(RoutedEventArgs* pArgs);

private:
    static void StaticConstructor();

    void BindProperty(DependencyObject* pDO, DependencyProperty* pDPFrom, DependencyProperty* pDPTo);
    static void OnTextChanged(DependencyObject* pDO, DependencyPropertyChangedEventArgs* e);

    TRefCountedPtr<TextBoxView> m_pTextBoxView;
    int m_caretIndex;
};


}; // namespace AVUI
